<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
	<title>Fancytree - Example: Events</title>

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js" type="text/javascript"></script>

	<link href="../src/skin-win7/ui.fancytree.css" rel="stylesheet" type="text/css">
	<script src="../src/jquery.fancytree.js" type="text/javascript"></script>

	<!-- Start_Exclude: This block is not part of the sample code -->
	<link href="../lib/prettify.css" rel="stylesheet">
	<script src="../lib/prettify.js" type="text/javascript"></script>
	<link href="sample.css" rel="stylesheet" type="text/css">
	<script src="sample.js" type="text/javascript"></script>
	<!-- End_Exclude -->

<script type="text/javascript">
	$.ui.fancytree.debugLevel = 1; // silence debug output

	function logEvent(event, data, msg){
//        var args = $.isArray(args) ? args.join(", ") :
		msg = msg ? ": " + msg : "";
		$.ui.fancytree.info("Event('" + event.type + "', node=" + data.node + ")" + msg);
	}
	$(function(){
		$("#tree").fancytree({
			checkbox: true,
			nolink: true,
			// Tree events
			init: function(event, data, flag) {
				logEvent(event, data, "flag=" + flag);
			},
			create: function(event, data, flag) {
				logEvent(event, data, "flag=" + flag);
			},
			// Node events
			queryactivate: function(event, data) {
				logEvent(event, data, "current state=" + data.node.isActive());
				// return false to prevent default behavior (i.e. activation)
//              return false;
			},
			activate: function(event, data) {
				logEvent(event, data);
				var node = data.node;
				// acces node attributes
				$("#echoActive").text(node.title);
				if( !$.isEmptyObject(node.data) ){
//					alert("custom node data: " + JSON.stringify(node.data));
				}
			},
			deactivate: function(event, data) {
				logEvent(event, data);
				$("#echoActive").text("-");
			},
			queryselect: function(event, data) {
				logEvent(event, data, "current state=" + data.node.isSelected());
				// return false to prevent default behavior (i.e. selecting or deselecting)
//				if( data.node.isFolder() ){
//					return false;
//				}
			},
			select: function(event, data) {
				logEvent(event, data, "current state=" + data.node.isSelected());
				var s = data.tree.getSelectedNodes().join(", ");
				$("#echoSelected").text(s);
			},
			queryexpand: function(event, data) {
				logEvent(event, data, "current state=" + data.node.isExpanded());
				// return false to prevent default behavior (i.e. expanding or collapsing)
//				return false;
			},
			collapse: function(event, data) {
				logEvent(event, data);
			},
			expand: function(event, data) {
				logEvent(event, data);
			},
			lazyload: function(event, data) {
				logEvent(event, data);
				// return children or any other node source
				data.result = [
					{title: "A Lazy node", lazy: true},
					{title: "Another node", selected: true}
					];
			},
			loadchildren: function(event, data) {
				logEvent(event, data);
			},
			focustree: function(event, data) {
				logEvent(event, data);
			},
			blurtree: function(event, data) {
				logEvent(event, data);
			},
			focus: function(event, data) {
				logEvent(event, data);
				$("#echoFocused").text(data.node.title);
			},
			blur: function(event, data) {
				logEvent(event, data);
				$("#echoFocused").text("-");
			},
			click: function(event, data) {
				logEvent(event, data, ", targetType=" + data.targetType);
				// return false to prevent default behavior (i.e. activation, ...)
				//return false;
			},
			dblclick: function(event, data) {
				logEvent(event, data);
//				data.node.toggleSelect();
			},
			keydown: function(event, data) {
				logEvent(event, data);
				switch( event.which ) {
				case 32: // [space]
					data.node.toggleSelected();
					return false;
				}
			},
			keypress: function(event, data) {
				logEvent(event, data);
			},
			createnode: function(event, data) {
				// Optionally tweak data.node.span or bind handlers here
				logEvent(event, data);
			},
			rendernode: function(event, data) {
				// Optionally tweak data.node.span
//              $(data.node.span).text(">>" + data.node.title);
				logEvent(event, data);
			}
		}).bind("fancytreeactivate", function(event, data){
			// alternative way to bind to 'activate' event
//		    logEvent(event, data);
		});

	});
</script>
</head>

<body class="example">
	<h1>Example: Events</h1>
	<div class="description">
		Implements all callbacks.
		<ul>
		<li>Use the Firebug plugin in Firefox to see the event log.
		<li>The 'links' folders contain nodes with an custom data.url option.
			This is used to open the URL in the onActivate event.
		</ul>
	</div>
	<div>
		<label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select>
	</div>

	<div id="tree">
		<ul>
			<li class="folder">jQuery links
				<ul>
					<li data-foo="bar">jQuery home
					<li data-json='{"url": "http://docs.jquery.com"}'>jQuery docs
				</ul>

			<li class="folder">Other links
				<ul>
					<li data-url="http://code.google.com">Google Code
				</ul>

			<li class="folder">Lazy loading
				<ul>
					<li id="k123" class="lazy">This is a lazy loading document with key k123.
					<li id="k234" class="lazy folder">This is a lazy loading folder with key k234.
				</ul>
		</ul>
	</div>

	<div>Active node: <span id="echoActive">-</span></div>
	<div>Selected node list: <span id="echoSelected">-</span></div>
	<div>Focused node: <span id="echoFocused">-</span></div>

	<!-- Start_Exclude: This block is not part of the sample code -->
	<hr>
	<p class="sample-links  no_code">
		<a class="hideInsideFS" href="http://dynatree.googlecode.com">jquery.dynatree.js project home</a>
		<a class="hideOutsideFS" href="#">Link to this page</a>
		<a class="hideInsideFS" href="index.html">Example Browser</a>
		<a href="#" id="codeExample">View source code</a>
	</p>
	<pre id="sourceCode" class="prettyprint" style="display:none"></pre>
	<!-- End_Exclude -->
</body>
</html>
